package com.atguigu.gulimall.cart.vo;

import java.math.BigDecimal;
import java.util.List;

/**
 * @create: 2024/9/6
 * @author: gyhe10
 * @description: 购物车模型类
 * 需要计算的属性，如总数量、类型数量、总价、减免价格等，都需要重写属性的 getter 方法，保证每次获取属性值时，经过计算获取
 **/

public class Cart {
    /**
     * 购物车所有购物项
     */
    private List<CartItem> items;
    /**
     * 购物车中商品总数量
     */
    private Integer countNum;
    /**
     * 购物车中商品类型数量
     */
    private Integer countType;
    /**
     * 购物车中商品总价格
     */
    private BigDecimal totalAmount;
    /**
     * 减免价格，默认 0
     */
    private BigDecimal reduce = new BigDecimal("0");

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                count += 1;
            }
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 遍历购物车所有购物项，计算总价格
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                if (item.getCheck()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        // 减去减免价格
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
